package com.ruoyi.web.controller.saas;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.saas.domain.CTenant;
import com.ruoyi.saas.dto.TenantConnectDTO;
import com.ruoyi.saas.enums.TenantStatusEnum;
import com.ruoyi.saas.service.ICTenantService;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.validation.constraints.NotNull;

/**
 * 租户信息Controller
 * 
 * @author ruoyi
 * @date 2021-11-02
 */
@RestController
@RequestMapping("/tenant/tenant")
public class CTenantController extends BaseController
{
    @Autowired
    private ICTenantService cTenantService;

    /**
     * 初始化
     */
    @ApiOperation(value = "连接数据源", notes = "连接数据源")
    @PostMapping("/initConnect")
    public AjaxResult initConnect(@Validated @RequestBody TenantConnectDTO tenantConnect) {
        return AjaxResult.success(cTenantService.connect(tenantConnect));
    }


    @ApiOperation(value = "检测租户是否存在", notes = "检测租户是否存在")
    @GetMapping("/check/{code}")
    public AjaxResult check(@PathVariable("code") String code) {
        return AjaxResult.success(cTenantService.check(code));
    }


    /**
     * 查询租户信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(CTenant cTenant)
    {
        startPage();
        List<CTenant> list = cTenantService.selectCTenantList(cTenant);
        return getDataTable(list);
    }

    /**
     * 导出租户信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:export')")
    @Log(title = "租户信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CTenant cTenant)
    {
        List<CTenant> list = cTenantService.selectCTenantList(cTenant);
        ExcelUtil<CTenant> util = new ExcelUtil<CTenant>(CTenant.class);
        return util.exportExcel(list, "租户信息数据");
    }

    /**
     * 获取租户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(cTenantService.selectCTenantById(id));
    }


    /**
     * 获取租户信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:info:query')")
    @GetMapping(value = "/selectCTenantByTenantCode/{tenantCode}")
    public AjaxResult selectCTenantByTenantCode(@PathVariable("tenantCode") String tenantCode)
    {
        return AjaxResult.success(cTenantService.selectCTenantByTenantCode(tenantCode));
    }

    /**
     * 新增租户信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:add')")
    @Log(title = "租户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CTenant cTenant)
    {
        return toAjax(cTenantService.insertCTenant(cTenant));
    }

    /**
     * 修改租户信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:edit')")
    @Log(title = "租户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CTenant cTenant)
    {
        return toAjax(cTenantService.updateCTenant(cTenant));
    }

    /**
     * 删除租户信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:remove')")
    @Log(title = "租户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cTenantService.deleteCTenantByIds(ids));
    }


    @PreAuthorize("@ss.hasPermi('system:info:edit')")
    @ApiOperation(value = "修改租户状态", notes = "修改租户状态")
    @Log(title = "租户信息", businessType = BusinessType.UPDATE)
    @PostMapping("/status/{ids}")
    public AjaxResult updateStatus(@PathVariable Long[] ids,
                                   @RequestParam(defaultValue = "FORBIDDEN") @NotNull(message = "状态不能为空") TenantStatusEnum status) {
        return AjaxResult.success(cTenantService.updateStatus(Arrays.asList(ids), status));
    }


}
